/*
 * $Id: CurriculumHourAction.java 3 2011-12-26 07:48:43Z forwardly@hotmail.com $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.as.web.programme;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.lightsaber.trade.as.dao.programme.BaseProgrammeDao;
import com.lightsaber.trade.as.dao.programme.StreamDao;
import com.lightsaber.trade.as.entity.programme.BaseProgramme;
import com.lightsaber.trade.as.entity.programme.CurriculumHour;
import com.lightsaber.trade.as.entity.programme.ProgrammeStatus;
import com.lightsaber.trade.as.service.programme.CurriculumHourManager;
import com.lightsaber.trade.core.web.BaseAction;

@Namespace("/programme")
public class CurriculumHourAction extends BaseAction {
    private static final long serialVersionUID = -6276043251380919769L;
    private String programmeId;
    
    @Autowired
    private transient BaseProgrammeDao baseProgrammeDao;
    
    @Autowired
    private CurriculumHourManager curriculumHourManager;

    @Autowired
    private StreamDao streamDao;
    
    private List<CurriculumHour> curriculumHours;
    
    private boolean canEdit;
    
    
    @Override
    public String execute() {
        return list();
    }

    public String list() {
        BaseProgramme programme =  baseProgrammeDao.get(programmeId);
        
        if(programme.getProgrammeKind().equals("Stream")){
            ProgrammeStatus status = streamDao.get(programmeId).getProgramme().getStatus();
            canEdit = null==status?true:status.isCanEdit();
         }else{
             canEdit = null==programme.getStatus()?true:programme.getStatus().isCanEdit();
         }
        curriculumHours = curriculumHourManager.findByProgrammeId(programmeId);

        return SUCCESS;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    public String save() throws Exception {
        if (curriculumHours != null && curriculumHours.size() > 0) {

            curriculumHourManager.save(curriculumHours);
        }

        addActionMessage(getText(PROCESSED_SUCCESS));
        return list();
    }

    public String getProgrammeId() {
        return programmeId;
    }

    public void setProgrammeId(final String programmeId) {
        this.programmeId = programmeId;
    }

    public List<CurriculumHour> getCurriculumHours() {
        return curriculumHours;
    }

    public void setCurriculumHours(final List<CurriculumHour> curriculumHours) {
        this.curriculumHours = curriculumHours;
    }

    public boolean isCanEdit() {
        return canEdit;
    }

    public void setCanEdit(boolean canEdit) {
        this.canEdit = canEdit;
    }
}
